package com.cqc.clippingbasic01;

import android.graphics.Outline;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        btn = (Button) findViewById(R.id.clip);


        ClipOutLineProvider lineProvider = new ClipOutLineProvider();
        tv.setOutlineProvider(lineProvider);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(tv.getClipToOutline()){
                    tv.setClipToOutline(false);
                }else{
                    tv.setClipToOutline(true);
                }
            }
        });
    }

    /**
     * minSdkVersion >= 21
     */
    private class ClipOutLineProvider extends ViewOutlineProvider {
        @Override
        public void getOutline(View view, Outline outline) {
            int margin = 80;
            int radius = 50;
//            outline.setRoundRect(0, 0, view.getWidth() - margin, view.getHeight() - margin, radius);
//            outline.setRoundRect(margin, margin, view.getWidth() - margin, view.getHeight() - margin, radius);

//            outline.setOval(0,0,view.getWidth(),view.getHeight());
//            outline.setOval(margin, margin, view.getWidth() - margin, view.getHeight() - margin);


            outline.setRect(0,0,view.getWidth() - margin, view.getHeight() - margin);
//            outline.setRect(margin, margin, view.getWidth() - margin, view.getHeight() - margin);
        }
    }
}
